package com.suisung.shopsuite.sys.service.impl;

import com.alibaba.fastjson.JSON;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.BasicSessionCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;
import com.suisung.shopsuite.common.pojo.dto.SmsDto;
import com.suisung.shopsuite.common.pojo.dto.UploadDto;
import com.suisung.shopsuite.sys.service.ThirdService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;


@Service
@Slf4j
public class TenServiceImpl implements ThirdService  {
    @Override
    public void send(SmsDto smsDto) {

    }

    @Override
    public String upload(UploadDto uploadDto) {
        String tmpSecretId = "AKIDXxDdx77wZ4Ws2ztiPuCH8qNgoleKm15z";
        String tmpSecretKey = "zsZ6c45wZv41rTs33qQmvsc9XlRwEJBp";
        String reg = "ap-nanjing";
        String secretId = tmpSecretId;//用户的 SecretId，建议使用子账号密钥，授权遵循最小权限指引，降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
        String secretKey = tmpSecretKey;//用户的 SecretKey，建议使用子账号密钥，授权遵循最小权限指引，降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
// 2 设置 bucket 的地域, COS 地域的简称请参见 https://cloud.tencent.com/document/product/436/6224
// clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。
        Region region = new Region(reg);
        ClientConfig clientConfig = new ClientConfig(region);
// 这里建议设置使用 https 协议
// 从 5.6.54 版本开始，默认使用了 https
        clientConfig.setHttpProtocol(HttpProtocol.https);
// 3 生成 cos 客户端。
        COSClient cosClient = new COSClient(cred, clientConfig);

// 指定文件将要存放的存储桶
        String bucketName = "pddsc-1343353974";
// 指定文件上传到 COS 上的路径，即对象键。例如对象键为 folder/picture.jpg，则表示将文件 picture.jpg 上传到 folder 路径下
        String key = "goods/" + System.currentTimeMillis() + ".jpg";
//        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentType("image/png");
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, uploadDto.getInputStream(), metadata);
        PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);

        log.info("UploadController upload putresult is {}", JSON.toJSONString(putObjectResult));
        //examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/images/picture.jpg
        return "https://" + bucketName + ".cos." + reg + ".myqcloud.com/" + key;
    }
}
